package com.googlecode.stockhamster.aiholder;

import com.googlecode.stockhamster.arbiter.IArbiter;

public class AI {
	private IArbiter arbiter;
	private Boolean valid;

	private String user, pass;
	private int com1, com2, com3, threshold;

	public AI(String username, String password, int com1, int com2, int com3,
			int threshold) {
		user = username;
		pass = password;
		this.com1 = com1;
		this.com2 = com2;
		this.com3 = com3;
		this.threshold = threshold;
		valid = true;
	}

	public void setArbiter(IArbiter arbiter) {
		this.arbiter = arbiter;
	}

	public String getUsername() {
		return user;
	}

	public String getPassword() {
		return pass;
	}

	public void work(AIEnvironment env) {
		if (!valid)
			return;
		String loginCode = arbiter.login(user, pass);
		arbiter.cleanRequestList(loginCode);
		Long balance = arbiter.getBalance(loginCode);
		System.out.println("Balance: " + balance);
		System.out.print("Stock amount: ");
		Integer stockAmount[] = new Integer[env.stockIdList.size()];
		for (int i = 0; i < stockAmount.length; i++) {
			stockAmount[i] = arbiter.getStockAmount(loginCode, env.stockIdList
					.get(i));
			System.out.print(stockAmount[i] + " ");
		}
		System.out.println();
		Long rightPrice[] = new Long[env.stockIdList.size()];
		for (int i = 0; i < stockAmount.length; i++)
			rightPrice[i] = (env.exptPrize1.get(i) * com1
					+ env.exptPrize2.get(i) * com2 + env.exptPrize3.get(i)
					* com3)
					/ (com1 + com2 + com3);
		for (int i = 0; i < stockAmount.length; i++)
			if (stockAmount[i] > 0) {
				Long sellPrice = rightPrice[i] * (100 + threshold) / 100;
				System.out.println("Sell stock " + env.stockIdList.get(i)
						+ " at price " + sellPrice);
				arbiter.requestSell(loginCode, env.stockIdList.get(i),
						sellPrice, stockAmount[i]);
			}
		Double totalPositiveRate = 0.0;
		Double interestRate[] = new Double[env.stockIdList.size()];
		for (int i = 0; i < stockAmount.length; i++) {
			interestRate[i] = (double) (rightPrice[i] - env.currPrize.get(i))
					/ env.currPrize.get(i);
			if (interestRate[i] > 0)
				totalPositiveRate += interestRate[i];
		}
		Long investment[] = new Long[env.stockIdList.size()];
		if (totalPositiveRate < 0.001)
			for (int i = 0; i < stockAmount.length; i++)
				investment[i] = balance / stockAmount.length;
		else
			for (int i = 0; i < stockAmount.length; i++)
				if (interestRate[i] > 0)
					investment[i] = Double.valueOf(
							(balance * (0.01 + (1 - 0.01 * stockAmount.length)
									* (interestRate[i] / totalPositiveRate))))
							.longValue();
				else
					investment[i] = Double.valueOf(balance * 0.01).longValue();
		for (int i = 0; i < stockAmount.length; i++) {
			Long buyPrice = rightPrice[i] * 100 / (100 + threshold);
			int amount = (int) (investment[i] / buyPrice);
			if (amount > 0) {
				System.out.println("Buy " + amount + " of stock "
						+ env.stockIdList.get(i) + " on price " + buyPrice);
				arbiter.requestBuy(loginCode, env.stockIdList.get(i), buyPrice,
						amount);
			}
		}
	}

	public void setValid(Boolean valid) {
		this.valid = valid;
	}

	public Boolean getValid() {
		return valid;
	}

}
